import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import '../../config/kq_pad_global.dart';
import '../../resources/images.dart';
import 'package:kq_flutter_core_widget/widgets/button/kq_ink_well.dart';
import '../../resources/kq_pad_theme_colors.dart';
import '../../resources/l10n/kq_pad_intl.dart';
import 'package:get/get.dart';
import '../../theme/kq_theme_manager.dart';
import '../divider/kq_pad_divider.dart';

/// 自定义弹窗
class KqCustomDialog extends AlertDialog {
  /// 标题
  final String? titleString;

  final Widget customWidget;

  /// 按钮的自定义布局
  final Widget? customBtnWidget;

  /// 左边按钮文字，默认"取消"
  final String? leftBtnText;

  /// 左边按钮文字，默认"确定"
  final String? rightBtnText;

  /// 左边按钮回调
  final Function()? onLeftBtnTap;

  /// 右边按钮回调
  final Function()? onRightBtnTap;

  /// 标题文字颜色
  final Color? titleColor;

  /// 左边按钮文字颜色
  final Color? leftBtnColor;

  /// 右边按钮文字颜色
  final Color? rightBtnColor;

  /// 标题文字大小
  final double? titleFontSize;

  /// 按钮文字
  final double? btnFontSize;

  //只显示左侧
  final bool isShowLeft;

  //只显示右侧
  final bool isShowRight;

  // 上边距
  final double? customTopPadding;

  // 下边距
  final double? customBottomPadding;

  const KqCustomDialog(
      {Key? key,
      this.titleString,
      required this.customWidget,
      this.customBtnWidget,
      this.leftBtnText,
      this.rightBtnText,
      this.titleColor,
      this.leftBtnColor,
      this.rightBtnColor,
      this.titleFontSize,
      this.btnFontSize,
      this.onLeftBtnTap,
      this.onRightBtnTap,
      this.customTopPadding,
      this.customBottomPadding,
      this.isShowLeft = false,
      this.isShowRight = false})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    // todo 平板布局写死横屏，竖屏要重写布局，用不到pad的组件库，所以不用判断横竖屏
    // bool isLangScape = context.orientation == Orientation.landscape;

    final List<Widget> children = <Widget>[];

    /// 上边距
    children.add(SizedBox(
      width: 1.r,
      height: customTopPadding ?? 18.r,
    ));

    /// 标题
    if (titleString != null) {
      children.add(Container(
        padding: EdgeInsets.symmetric(horizontal: 8.r),
        alignment: Alignment.center,
        child: Text(
          titleString!,
          style: TextStyle(
              fontSize: 8.sp,
              color: KqPadThemeColors.text26,
              fontWeight: FontWeight.w600),
        ),
      ));
      children.add(SizedBox(
        width: 1.r,
        height: 12.r,
      ));
    }

    //自定义内容
    children.add(customWidget);

    /// 下边距
    children.add(SizedBox(
      width: 1.r,
      height: customBottomPadding ?? 19.r,
    ));

    /// 底部按钮
    children.add(KqPadDivider(
      height: 1.r,
    ));

    children.add(customBtnWidget ??
        Row(
          children: [
            //不设置左右 都展示
            if (isShowLeft || (!isShowLeft && !isShowRight))
              Expanded(
                  child: KqInkWell(
                onTap: () {
                  if (onLeftBtnTap != null) {
                    onLeftBtnTap!();
                  }
                },
                child: Container(
                  alignment: Alignment.center,
                  width: double.infinity,
                  height: 22.r,
                  child: Text(
                    leftBtnText ?? KqPadIntl.currentResource.backPrevious,
                    style: TextStyle(
                        fontSize: 8.sp,
                        color: leftBtnColor ?? KqPadThemeColors.text8C),
                  ),
                ),
              )),
            if ((!isShowLeft && !isShowRight) || (isShowLeft && isShowRight))
              KqPadVerticalDivider(
                height: 22.r,
              ),
            //不需要强制
            //需要强制  并且左边的没传数据
            if (isShowRight || (!isShowLeft && !isShowRight))
              Expanded(
                  child: KqInkWell(
                onTap: () {
                  if (onRightBtnTap != null) {
                    onRightBtnTap!();
                  }
                },
                child: Container(
                  alignment: Alignment.center,
                  width: double.infinity,
                  height: 22.r,
                  child: Text(
                    rightBtnText ?? KqPadIntl.currentResource.cancel,
                    style: TextStyle(
                        fontSize: 8.sp,
                        color: rightBtnColor ?? KqPadThemeColors.textLightBlue),
                  ),
                ),
              ))
          ],
        ));

    Widget dialogChild = Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: children,
    );
    var padding = MediaQuery.of(context).viewInsets.bottom;
    return UnconstrainedBox(
        child: Container(
            padding: EdgeInsets.only(bottom: padding),
            width: 160.r,
            child: Material(
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(Radius.circular(8.r))),
                color: KqPadThemeColors.bgWhite,
                child: ClipRRect(
                  borderRadius: BorderRadius.all(Radius.circular(8.r)),
                  child: dialogChild,
                ))));
  }
}
